package com.guandata.cdc.model.source;

import com.guandata.cdc.model.ConnectParam;
import com.guandata.cdc.model.DataTableType;
import lombok.Getter;
import lombok.NonNull;

import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

/**
 * @author daishuyuan
 */
@Getter
public enum SourceConnectParam implements ConnectParam {
    HOST_NAME(
        "hostname",
        Set.of(DataTableType.POSTGRES, DataTableType.MYSQL),
        params -> true
    ),
    PORT(
        "port",
        Set.of(DataTableType.POSTGRES, DataTableType.MYSQL),
        params -> true
    ),
    SERVER_ID(
            "server-id",
            Set.of(DataTableType.MYSQL),
            params -> true
    ),
    DATABASE_NAME(
        "database-name",
        Set.of(DataTableType.POSTGRES, DataTableType.MYSQL),
        params -> true
    ),
    SCHEMA_NAME(
        "schema-name",
        Set.of(DataTableType.POSTGRES),
        params -> true
    ),
    TABLE_NAME(
        "table-name",
        Set.of(DataTableType.POSTGRES, DataTableType.MYSQL),
        params -> true
    ),
    USERNAME(
        "username",
        Set.of(DataTableType.POSTGRES, DataTableType.MYSQL),
        params -> true
    ),
    PASSWORD(
        "password",
        Set.of(DataTableType.POSTGRES, DataTableType.MYSQL),
        params -> true
    ),
    SLOT_NAME(
        "slot.name",
        Set.of(DataTableType.POSTGRES),
        params -> true
    ),
    DECODING_PLUGIN_NAME(
        "decoding.plugin.name",
        Set.of(DataTableType.POSTGRES),
        params -> true
    ),
    SCAN_STARTUP_MODE(
        "scan.startup.mode",
        Set.of(DataTableType.POSTGRES, DataTableType.MYSQL),
        params -> true
    ),
    SCAN_INCREMENTAL_SNAPSHOT_ENABLED(
        "scan.incremental.snapshot.enabled",
        Set.of(DataTableType.POSTGRES),
        params -> true
    ),
    SCAN_INCREMENTAL_SNAPSHOT_CHUNK_SIZE(
        "scan.incremental.snapshot.chunk.size",
        Set.of(DataTableType.POSTGRES),
        params -> Boolean.valueOf(
            Optional
                .ofNullable(params.get(SCAN_INCREMENTAL_SNAPSHOT_ENABLED))
                .orElse(Boolean.FALSE.toString())
        )
    );

    private final String param;
    private final Set<DataTableType> dataTableTypes;
    private final Function<Map<ConnectParam, String>, Boolean> preChecker;

    SourceConnectParam(
        @NonNull String param,
        @NonNull Set<DataTableType> dataTableTypes,
        @NonNull Function<Map<ConnectParam, String>, Boolean> preChecker
    ) {
        this.param = param;
        this.dataTableTypes = dataTableTypes;
        this.preChecker = preChecker;
    }
}
